import time
import allure
import re
from config.setting import host_port
from pages.basePage2 import BasePage

#元素定位层
class OwnerPage(BasePage):
    def __init__(self):
        BasePage.__init__(self)
        time.sleep(2)
        self.driver.get(f"http://{host_port}/SharedParkingPlace/lessor/index.html")

    # 定位 表单
    def get_frame_element(self):
        return self.driver.find_element_by_id('testIframe')

    #定位 我的按钮
    def get_owner_element(self):
        return self.driver.find_element_by_link_text('我的')

    #定位 用户个人信息
    def get_ownerInfo_elements(self):
        return self.driver.find_elements_by_xpath('/html/body/div/div[2]/div[1]/div/div/div[1]/div[2]/div[1]/h3/p')

    #定位 更改个人信息按钮
    def get_alterOwnerInfo_element(self):
        return self.driver.find_element_by_xpath('//*[@id="main"]/div[2]/div[1]/div/div/div[1]/div[2]/div[1]/h3/button[1]')

    #定位 用户姓名输入框
    def get_uname_element(self):
        return self.driver.find_element_by_xpath('//input[@id="uname"]')

    #定位 用户电话输入框
    def get_phone_element(self):
        return self.driver.find_element_by_xpath('//input[@id="phone"]')

    #定位 用户昵称输入框
    def get_nickname_element(self):
        return self.driver.find_element_by_xpath('//input[@id="nickname"]')

    #定位 用户性别输入框
    def get_sex_element(self):
        return self.driver.find_element_by_xpath('//input[@id="sex"]')

    #定位 提交更改按钮
    def get_commitalter_element(self):
        return self.driver.find_element_by_xpath('//div[@id="userinfo"]//div[@align="center"]/button[2]')

    #定位 阿里账户
    def get_aliaccount_element(self):
        return self.driver.find_element_by_xpath('/html/body/div/div[2]/div[1]/div/div/div[2]/div[2]/div/h3/p[3]')

    #定位 变更账户信息按钮
    def get_changeAccount_element(self):
        return self.driver.find_element_by_xpath('/html/body/div/div[2]/div[1]/div/div/div[2]/div[2]/div/h3/button[1]')

    #定位 阿里账户输入框
    def get_inputAccount_element(self):
        return self.driver.find_element_by_id('bindalipay')

    #定位 提交更改按钮
    def get_commitChangebtn_element(self):
        return self.driver.find_element_by_xpath('//div[@id="account"]//div[@align="center"]/button[2]')

    #定位 提现按钮
    def get_Withdrawalbtn_element(self):
        return self.driver.find_element_by_xpath('/html/body/div/div[2]/div[1]/div/div/div[2]/div[2]/div/h3/button[3]')

    #定位 提现金额输入框
    def get_amount_element(self):
        return self.driver.find_element_by_id('WIDtotal_amount')

    #定位 提交提现按钮
    def get_commitWithdrawal_element(self):
        return self.driver.find_element_by_xpath('//dd[@id="btn-dd"]/span/button')

    #定位 账户余额
    def get_Accountbalance_element(self):
        return self.driver.find_element_by_xpath('/html/body/div/div[2]/div[1]/div/div/div[2]/div[2]/div/h3/p[2]')

    #定位 直接返回按钮
    def get_back_element(self):
        return self.driver.find_element_by_link_text('直接返回')

#操作层
class OwnerHandle(OwnerPage):
    def __init__(self):
        OwnerPage.__init__(self)

    @allure.step('切换表单')
    def switch_frame(self):
        frame_element = self.get_frame_element()
        self.driver.switch_to.frame(frame_element)

    @allure.step('点击我的按钮')
    def click_ownerbtn(self):
        self.get_owner_element().click()

    @allure.step('获取个人信息列表')
    def get_ownerInfo(self):
        ownerElements = self.get_ownerInfo_elements()
        ownerinfo_list = []
        for ownerElement in ownerElements:
            ownerinfo_list.append(ownerElement.text)
        return ownerinfo_list

    @allure.step('点击 修改个人信息按钮')
    def click_alterOwnerInfo(self):
        self.get_alterOwnerInfo_element().click()

    @allure.step('输入用户姓名')
    def input_uname(self, uname):
        unamebox = self.get_uname_element()
        unamebox.clear()
        unamebox.send_keys(uname)

    @allure.step('输入用户电话')
    def input_phone(self, phone):
        phonebox = self.get_phone_element()
        phonebox.clear()
        phonebox.send_keys(phone)

    @allure.step('输入用户昵称')
    def input_nickname(self, nickname):
        nicknamebox = self.get_nickname_element()
        nicknamebox.clear()
        time.sleep(0.5)
        nicknamebox.send_keys(nickname)

    @allure.step('输入用户性别')
    def input_sex(self, sex):
        sexbox = self.get_sex_element()
        sexbox.clear()
        sexbox.send_keys(sex)

    @allure.step('点击 提交更改按钮')
    def click_commitalter(self):
        self.get_commitalter_element().click()

    @allure.step('获取 阿里账户')
    def get_aliaccount(self):
        return self.get_aliaccount_element().text

    @allure.step('点击 变更账户操作按钮')
    def click_changeAccount(self):
        self.get_changeAccount_element().click()

    @allure.step('输入 阿里账户')
    def input_Account(self, account):
        account_box = self.get_inputAccount_element()
        account_box.clear()
        account_box.send_keys(account)

    @allure.step('点击 提交更改按钮')
    def click_commitChange(self):
        self.get_commitChangebtn_element().click()

    @allure.step('点击 提现按钮')
    def click_Withdrawalbtn(self):
        self.get_Withdrawalbtn_element().click()

    @allure.step('输入 提现金额')
    def input_amount(self, amount):
        amount_box = self.get_amount_element()
        amount_box.clear()
        amount_box.send_keys(amount)

    @allure.step('点击 提交提现按钮')
    def click_commitWithdrawl(self):
        self.get_commitWithdrawal_element().click()

    @allure.step('获取 账户余额')
    def get_Accountbalance(self):
        balance = self.get_Accountbalance_element().text
        balance1 = balance.strip().split(':')[1]
        return balance1

    @allure.step('点击 直接返回按钮')
    def click_back(self):
        self.get_back_element().click()

class OwnerBusiness(OwnerHandle):
    def __init__(self):
        OwnerHandle.__init__(self)

    #更改个人信息
    def alterOwnerInfo(self, uname, phone, nickname, sex):
        #我的--获取个人信息--更改个人信息--姓名--电话--昵称--性别--提交更改--获取个人信息
        self.click_ownerbtn()
        self.switch_frame()
        self.click_alterOwnerInfo()
        self.input_uname(uname)
        self.input_phone(phone)
        self.input_nickname(nickname)
        self.input_sex(sex)
        time.sleep(1)
        self.click_commitalter()
        time.sleep(2)

        #获取个人信息后加工整理干净
        ownerInfo = self.get_ownerInfo()
        str_ownerInfo = ",".join(ownerInfo) + ','
        p = re.compile(r":\s(\S+),")
        newownerInfo = p.findall(str_ownerInfo)
        return newownerInfo

    #更改账户信息
    def changeAccountInfo(self ,account):
        #点击我的--切换表单--变更账户信息--输入账户--提交更改--获取当前阿里账户
        self.click_ownerbtn()
        self.switch_frame()
        self.click_changeAccount()
        self.input_Account(account)
        self.click_commitChange()
        time.sleep(1)
        aliaccount = self.get_aliaccount()
        return aliaccount

    #提现
    def withdrawal(self, amount):
        #点击我的--点击提现--切换表单--输入金额--点击提现--获取账户余额
        self.click_ownerbtn()
        self.switch_frame()
        balance = self.get_Accountbalance()
        self.click_Withdrawalbtn()
        self.input_amount(amount)
        self.click_commitWithdrawl()
        time.sleep(1)
        self.click_back()
        time.sleep(1)
        self.click_ownerbtn()
        self.switch_frame()
        newbalance = self.get_Accountbalance()
        return balance, newbalance


if __name__ == "__main__":
    o1 = OwnerBusiness()
    # a = o1.alterOwnerInfo('出租方1','11112341234', '大猩猩' ,'0')
    # a = o1.changeAccountInfo('18812341232')
    a , b = o1.withdrawal('20')
    print(a, b)

